package utils.data 
{
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	/**
	 * 解析VO数据
	 * @author WLDragon 2014-02-23
	 */
	public class ParseVOData implements IParseData
	{
		
		public function ParseVOData() 
		{
			
		}
		
		public function parseData(bytes:ByteArray):Dictionary 
		{
			var dic:Dictionary = new Dictionary();
			bytes.position = 0;
			while (bytes.bytesAvailable)
			{
				var key:String = bytes.readUTF();
				var len:uint = bytes.readUnsignedInt();
				var data:String = bytes.readUTFBytes(len);
				var table:Array = data.split('\n');
				var n:int = table.length - 3;//因为table前两行不是数据内容，所以也要减掉
				var names:Array = (table.shift() as String).split('\t');
				var types:Array = (table.shift() as String).split('\t');
				var l:int = names.length - 1;//因为用\t分割的最后一位是""，所以长度要减1
				dic[key] = new Dictionary();
				for (var j:int = 0; j < n;j++ )
				{
					var r:Array = String(table[j]).split('\t');
					var classRef:Class = getDefinitionByName("vo." + key) as Class;
					var o:Object = new classRef();
					for (var i:int = 0; i < l;i++ )
					{
						if (types[i] == "Array")
						{
							var a:Array = (r[i] as String).split(',');
							if (a[0] == "")
								a.length = 0;
							o[names[i]] = a;
						}
						else
						{
							o[names[i]] = r[i];
						}
					}
					dic[key][int(o['id'])] = o;
				}
			}
			
			return dic;
		}
		
	}

}